/** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
**/


var RightMenu = function (iconid, popupid, containerid, buttonscontainerid) {
	this.icon = get(iconid);
	this.popup = get(popupid);
	this.container = get(containerid);
	this.buttonscontainer = get(buttonscontainerid);
	
	this.animation = false;
	
	var self = this;	
	this.popup[0].onclick = function (e) { self.click(e); };
    this.popup[0].onmouseenter = function (e) { self.mousemove(e); };
	this.popup[0].onmousemove = function (e) { self.mousemove(e); };
	this.popup[0].onmouseout = function (e) { self.mouseout(e); };
	
	this.enable();
	
	this.reset();
}

RightMenu.prototype.enable = function () {
	var self = this;
	this.disable();
	
	this.icon[0].onclick = function(){
		highlight(self.icon, function(){
			if (self.popup.hasClass("hidden")) 
				self.show();
			else 
				self.hide();
		})
	};
}

RightMenu.prototype.disable = function () {
	this.icon.unbind("click");
}

RightMenu.prototype.enableAnimation = function(enable) {
	this.animation = enable;
}

RightMenu.prototype.show = function () {
	if (this.buttonscontainer.children().length) {
		var first = this.buttonscontainer.children()[0];
		this.icon.data("down", { obj: $(first) });
	} else {
		this.icon.data("down", null);
	}
	
	this.popup.removeClass("hidden");
	if(this.animation) {
		this.container.stop();
		this.container.css("right", (-this.container.width()) + "px");
		this.container.animate({
			right: "0px"
		}, 1000);
	}
	
	var self = this;
}


RightMenu.prototype.hide = function (gotoTab) {
	this.icon.data("down", null);
	
	this.icon.removeClass("selected"); 
	this.buttonscontainer.find(".selected").removeClass("selected");
	
	if (this.animation) {
		this.container.stop();
		var self = this;
		this.container.animate({
			right: (-this.container.width()) + "px"
		}, 1000, function(){
			self.popup.addClass("hidden");
			if(gotoTab) self.selectTab();
		});
	}
	else {
		this.popup.addClass("hidden");
		if(gotoTab) this.selectTab();
	}
}

RightMenu.prototype.selectTab = function () {
	var sel = $("#tabs a.selected");
}

RightMenu.prototype.reset = function () {
	this.buttonscontainer.empty();
}

RightMenu.prototype.addButton = function (name, callback, icon, toggleEnabled, toggleId, toggleOn) {
	
	var menuItem = $('<a href="#" class="soft_menu_item_cell">');
	
	var itemText = $('<div ' + (toggleId ? 'id="' + toggleId : '') + '" class="soft_menu_button">');
	
	menuItem.mouseout(function() {
		$(this).removeClass('pressed');
	});
	
	if (icon) {
		menuItem.append('<img class = "soft_menu_item_icon" src="' + icon + '" />');
	}
	
	if (toggleEnabled) {
		itemText.text(name + ' ' + (toggleOn ? str_toggle_on : str_toggle_off));
	} else {
		itemText.text(name);		
	}
	
	menuItem.data("callback", callback);
	
	var self = this;
	menuItem.data("left", { obj: this.rightIcon, removeSelected: true, onSelect: function () { self.hide(true); } });
	
	var children = this.buttonscontainer.children();
	if(children.length) {
		var child = $(children[children.length-1]);
		child.data("down", { obj: menuItem, removeSelected: true });
		menuItem.data("up", { obj: child, removeSelected: true });
	}
	
	menuItem.append(itemText);
	
	this.buttonscontainer.append(menuItem);
}

RightMenu.prototype.click= function (e) {
 	var button = $(e.target).closest("a");
	
	if (button && button.length) {
		this.container.find("a.selected").removeClass("selected");
		highlight(button, function() { 		
			if(button.data("callback")) {
				button.data("callback").call(this);
			}		
		} );
		return;
	}
	
	button = $(e.target).closest("img");
	
	if (button && button.length) {
		if (button.data("callback")) {
			button.data("callback").call(this);
			return;
		}
	}			
	
	this.hide(true);
}

RightMenu.prototype.mousemove= function (e) {
 	var button = $(e.target).closest("a");
	
	if (button && button.length) {
		this.container.find("a.selected").removeClass("selected");
		this.container.find("a.pressed").removeClass("pressed");
		button.addClass("pressed");
		return;
	}
}

RightMenu.prototype.mouseout= function (e) {
 	var button = $(e.target).closest("a");
	
	if (button && button.length) {
		this.container.find("a.selected").removeClass("selected");
		this.container.find("a.pressed").removeClass("pressed");
		return;
	}
}
